package com.wnxy.test;

import com.wnxy.mapper.BookMapper;
import com.wnxy.pojo.Book;
import com.wnxy.repository.BookRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;

import java.util.List;

@SpringBootTest
public class App1_index {
    @Autowired
    private BookMapper bookMapper;
    @Autowired
    private BookRepository bookRepository;

    // 查询
    @Test
    public void getBooks(){
        List<Book> list = bookMapper.findAll();
        list.forEach(System.out::println);
    }
    // 把从MySQL查询的数据，同步到索引库
    @Test
    public void createIndexAndAddDocument(){
        List<Book> list = bookMapper.findAll();
        // 把查询到的数据插入到索引库，如果索引库不存在会自动创建
        bookRepository.saveAll(list);
    }

    // 测试命名查询
    @Test
    public void findByName(){
        List<Book> list = bookRepository.findByBookname("神");
        list.forEach(System.out::println);
    }

    // 测试其他查询，如分页：
    @Test
    public void findByPage(){
        // 注意：页码从0开始
        PageRequest pageRequest = PageRequest.of(0, 2);
        Page<Book> list = bookRepository.findAll(pageRequest);
        System.out.println("总记录数 = " + list.getTotalElements());
        System.out.println("总页数 = " + list.getTotalPages());
        System.out.println("当前页数据 = " + list.getContent());
    }
}
